Zip for WinRT
Retrieving Images from a Zip File

In this example, two buttons and a listbox will be used to show how images can be retrieved from a .zip file.

To retrieve images directly from a zip file, first add the following code to compress several image files into a zip file. In this example, the code is added to the btnNew_Click event, which creates a new .zip file for the images when a button is clicked.

C#
Copy Code
private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            // Show open file dialog.
            SaveFileDialog dlgSaveFile = new SaveFileDialog();
            dlgSaveFile.Filter = "Zip Files (*.zip) | *.zip";
            // Open zip file.
            try
            {
                if (dlgSaveFile.ShowDialog() == true)
                {
                    zipFile.Create(dlgSaveFile.OpenFile());
                }
            }
            catch
            {
                MessageBox.Show("Can't create a ZIP file, please try again.", "C1Zip", MessageBoxButton.OK);
            }
        }

Then use the following code to add image files to a list in the ListBox.

C#
Copy Code
 private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Get list of files to add.
            OpenFileDialog fo = new OpenFileDialog();
            fo.Multiselect = true;
            if (fo.ShowDialog() == true)
            {
                // Add files in the list.
                foreach (FileInfo file in fo.Files)
                {
                    Stream stream = file.OpenRead();
                    listBox1.Items.Add(file.Name);
                    zipFile.Entries.Add(stream, file.Name);
                }
            }
        }

To allow you to select an image, retrieve a stream with the image data (C1ZipEntry.OpenReader method), and add the following code to the listBox1_SelectionChanged and StreamCopy events:

C#
Copy Code
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Get the selected item.
            string item = (string)listBox1.SelectedItem;
            // Load the image directly from a compressed stream.
            Stream stream = zipFile.Entries[item].OpenReader();
            using (MemoryStream ms = new MemoryStream())
            {
                StreamCopy(ms, stream);
                BitmapImage img = new BitmapImage();
                img.SetSource(ms);
                this.image1.Source = img;
                // Done with stream.
                stream.Close();
            }
        }
        private void StreamCopy(Stream dstStream, Stream srcStream)
        {
            byte[] buffer = new byte[32768];
            for (; ; )
            {
                int read = srcStream.Read(buffer, 0, buffer.Length);
                if (read == 0) break;
                dstStream.Write(buffer, 0, read);
            }
            dstStream.Flush();
        }

This topic illustrates the following:

This example shows several types of images, including ICO, TIFF, BMP, and JPG images.

 

See Also

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback